(function() {
  var express, fs, iced, pgsql, router, table_const, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  fs = require('fs');

  pgsql = require('pg');

  table_const = require('../lib/tables');

  router = express.Router();

  module.exports = router;

  router.get('/', function(req, res, next) {
    return res.redirect('/audit/merch');
  });

  router.all('/merch', function(req, res, next) {
    var append, client, done, err, field, k, merch, offset, old_v, page_size, result, thread, v, where_cond, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.category = 'merch';
    res.locals.page_no = parseInt(req.body.page_no) || 1;
    page_size = 10;
    offset = (res.locals.page_no - 1) * page_size;
    where_cond = "(json_extract_path_text(a.new_data, 'merno') like $1 or\n json_extract_path_text(a.new_data, 'name') like $1)";
    if (req.body.start_time && req.body.start_time.length > 0) {
      where_cond += " and a.timestamp >= '" + req.body.start_time + "'";
      res.locals.start_time = req.body.start_time;
    }
    if (req.body.end_time && req.body.end_time.length > 0) {
      where_cond += " and a.timestamp <= '" + req.body.end_time + "'";
      res.locals.end_time = req.body.end_time;
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/audit.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 36
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/audit.coffee"
          });
          client.query("select json_extract_path_text(a.new_data, 'uuid') as uuid from web_audit as a where a.table_name = 'pay_merch' and " + where_cond + " group by uuid", ["%" + res.locals.keyword + "%"], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 46
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.page_nums = Math.floor((result.rows.length + page_size - 1) / page_size);
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/audit.coffee"
            });
            client.query("select distinct on(uuid) json_extract_path_text(a.new_data, 'uuid') as uuid, json_extract_path_text(a.new_data, 'merno') as merno, json_extract_path_text(a.new_data, 'name') as name from web_audit as a where a.table_name = 'pay_merch' and " + where_cond + " order by uuid, a.timestamp desc offset $2 limit $3", ["%" + res.locals.keyword + "%", offset, page_size], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 61
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            res.locals.merchs = result.rows;
            (function(__iced_k) {
              var _i, _len, _ref, _results, _while;
              _ref = res.locals.merchs;
              _len = _ref.length;
              _i = 0;
              _while = function(__iced_k) {
                var _break, _continue, _next;
                _break = __iced_k;
                _continue = function() {
                  return iced.trampoline(function() {
                    ++_i;
                    return _while(__iced_k);
                  });
                };
                _next = _continue;
                if (!(_i < _len)) {
                  return _break();
                } else {
                  merch = _ref[_i];
                  (function(__iced_k) {
                    __iced_deferrals = new iced.Deferrals(__iced_k, {
                      parent: ___iced_passed_deferral,
                      filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/audit.coffee"
                    });
                    client.query("select a.*, u.realname as input_user, u2.realname as check_user, u3.realname as old_input_user, u4.realname as old_check_user, f.name as fee_name, f2.name as old_fee_name, m2.merno as mapto_merno, m2.name as mapto_mer_name, m3.merno as old_mapto_merno, m3.name as old_mapto_mer_name, m4.merno as settl_merno, m4.name as settl_mer_name, m5.merno as old_settl_merno, m5.name as old_settl_mer_name from web_audit as a left join web_users as u on u.uuid = json_extract_path_text(a.new_data, 'inputer') left join web_users as u2 on u2.uuid = json_extract_path_text(a.new_data, 'checker') left join web_users as u3 on u3.uuid = json_extract_path_text(a.old_data, 'inputer') left join web_users as u4 on u4.uuid = json_extract_path_text(a.old_data, 'checker') left join pay_fee as f on f.uuid = json_extract_path_text(a.new_data, 'fee') left join pay_fee as f2 on f2.uuid = json_extract_path_text(a.old_data, 'fee') left join pay_merch as m2 on m2.uuid = json_extract_path_text(a.new_data, 'mapto_merid') left join pay_merch as m3 on m3.uuid = json_extract_path_text(a.old_data, 'mapto_merid') left join pay_merch as m4 on m4.uuid = json_extract_path_text(a.new_data, 'settl_merid') left join pay_merch as m5 on m5.uuid = json_extract_path_text(a.old_data, 'settl_merid') where " + where_cond + " and json_extract_path_text(a.new_data, 'uuid') = $2 order by a.timestamp desc", ["%" + res.locals.keyword + "%", merch.uuid], __iced_deferrals.defer({
                      assign_fn: (function() {
                        return function() {
                          err = arguments[0];
                          return result = arguments[1];
                        };
                      })(),
                      lineno: 101
                    }));
                    __iced_deferrals._fulfill();
                  })(function() {
                    var _j, _k, _len1, _len2, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6;
                    if (err) {
                      done(client);
                      return next(err);
                    }
                    merch.threads = [];
                    _ref1 = result.rows;
                    for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
                      thread = _ref1[_j];
                      thread.modified_fields = [];
                      if (thread.action === 'I') {
                        _ref2 = thread.new_data;
                        for (k in _ref2) {
                          v = _ref2[k];
                          if (v !== null && v.toString().length > 0) {
                            thread.modified_fields.push({
                              key: k,
                              new_v: v.toString()
                            });
                          }
                        }
                      } else if (thread.action === 'D') {
                        _ref3 = thread.old_data;
                        for (k in _ref3) {
                          v = _ref3[k];
                          if (v !== null && v.toString().length > 0) {
                            thread.modified_fields.push({
                              key: k,
                              old_v: v.toString()
                            });
                          }
                        }
                      } else if (thread.action === 'U') {
                        _ref4 = thread.new_data;
                        for (k in _ref4) {
                          v = _ref4[k];
                          v = v === null ? '' : v.toString();
                          old_v = thread.old_data[k] === null ? '' : thread.old_data[k].toString();
                          if (old_v !== v) {
                            thread.modified_fields.push({
                              key: k,
                              new_v: v,
                              old_v: old_v
                            });
                          }
                        }
                      }
                      if (thread.modified_fields.length === 0) {
                        continue;
                      }
                      append = false;
                      _ref5 = thread.modified_fields;
                      for (_k = 0, _len2 = _ref5.length; _k < _len2; _k++) {
                        field = _ref5[_k];
                        if (!((_ref6 = field.key) === 'order_id' || _ref6 === 'trace_num')) {
                          append = true;
                          break;
                        }
                      }
                      if (append) {
                        merch.threads.push(thread);
                      }
                    }
                    return _next();
                  });
                }
              };
              _while(__iced_k);
            })(function() {
              done();
              res.locals.pay_merch = table_const.pay_merch;
              return res.render('audit/index');
            });
          });
        });
      };
    })(this));
  });

  router.all('/terms', function(req, res, next) {
    var append, client, done, err, field, k, offset, old_v, page_size, result, term, thread, v, where_cond, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.category = 'terms';
    res.locals.page_no = parseInt(req.body.page_no) || 1;
    page_size = 10;
    offset = (res.locals.page_no - 1) * page_size;
    where_cond = "(json_extract_path_text(a.new_data, 'termid') like $1)";
    if (req.body.start_time && req.body.start_time.length > 0) {
      where_cond += " and a.timestamp >= '" + req.body.start_time + "'";
      res.locals.start_time = req.body.start_time;
    }
    if (req.body.end_time && req.body.end_time.length > 0) {
      where_cond += " and a.timestamp <= '" + req.body.end_time + "'";
      res.locals.end_time = req.body.end_time;
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/audit.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 158
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/audit.coffee"
          });
          client.query("select json_extract_path_text(a.new_data, 'uuid') as uuid from web_audit as a where a.table_name = 'pay_terms' and " + where_cond + " group by uuid", ["%" + res.locals.keyword + "%"], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 168
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.page_nums = Math.floor((result.rows.length + page_size - 1) / page_size);
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/audit.coffee"
            });
            client.query("select distinct on(uuid) json_extract_path_text(a.new_data, 'uuid') as uuid, json_extract_path_text(a.new_data, 'termid') as termid, m.merno as merno, m.name as mer_name, m.inside as mer_inside, m.chnl as mer_chnl from web_audit as a left join pay_merch as m on json_extract_path_text(a.new_data, 'merid') = m.uuid where a.table_name = 'pay_terms' and " + where_cond + " order by uuid, a.timestamp desc offset $2 limit $3", ["%" + res.locals.keyword + "%", offset, page_size], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 186
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            res.locals.terms = result.rows;
            (function(__iced_k) {
              var _i, _len, _ref, _results, _while;
              _ref = res.locals.terms;
              _len = _ref.length;
              _i = 0;
              _while = function(__iced_k) {
                var _break, _continue, _next;
                _break = __iced_k;
                _continue = function() {
                  return iced.trampoline(function() {
                    ++_i;
                    return _while(__iced_k);
                  });
                };
                _next = _continue;
                if (!(_i < _len)) {
                  return _break();
                } else {
                  term = _ref[_i];
                  (function(__iced_k) {
                    __iced_deferrals = new iced.Deferrals(__iced_k, {
                      parent: ___iced_passed_deferral,
                      filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/audit.coffee"
                    });
                    client.query("select a.*, u.realname as input_user, u2.realname as check_user, u3.realname as old_input_user, u4.realname as old_check_user, t2.termid as mapto_termid, t3.termid as old_mapto_termid, t4.termid as assoc_termid, t5.termid as old_assoc_termid, m.merno as merno, m.name as mer_name, m2.merno as old_merno, m2.name as old_mer_name from web_audit as a left join web_users as u on u.uuid = json_extract_path_text(a.new_data, 'inputer') left join web_users as u2 on u2.uuid = json_extract_path_text(a.new_data, 'checker') left join web_users as u3 on u3.uuid = json_extract_path_text(a.old_data, 'inputer') left join web_users as u4 on u4.uuid = json_extract_path_text(a.old_data, 'checker') left join pay_terms as t2 on t2.uuid = json_extract_path_text(a.new_data, 'mapto_term') left join pay_terms as t3 on t3.uuid = json_extract_path_text(a.old_data, 'mapto_term') left join pay_terms as t4 on t4.uuid = json_extract_path_text(a.new_data, 'assoc_term') left join pay_terms as t5 on t5.uuid = json_extract_path_text(a.old_data, 'assoc_term') left join pay_merch as m on m.uuid = json_extract_path_text(a.new_data, 'merid') left join pay_merch as m2 on m2.uuid = json_extract_path_text(a.old_data, 'merid') where " + where_cond + " and json_extract_path_text(a.new_data, 'uuid') = $2 order by a.timestamp desc", ["%" + res.locals.keyword + "%", term.uuid], __iced_deferrals.defer({
                      assign_fn: (function() {
                        return function() {
                          err = arguments[0];
                          return result = arguments[1];
                        };
                      })(),
                      lineno: 225
                    }));
                    __iced_deferrals._fulfill();
                  })(function() {
                    var _j, _k, _len1, _len2, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6;
                    if (err) {
                      done(client);
                      return next(err);
                    }
                    term.threads = [];
                    _ref1 = result.rows;
                    for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
                      thread = _ref1[_j];
                      thread.modified_fields = [];
                      if (thread.action === 'I') {
                        _ref2 = thread.new_data;
                        for (k in _ref2) {
                          v = _ref2[k];
                          if (v !== null && v.toString().length > 0) {
                            thread.modified_fields.push({
                              key: k,
                              new_v: v.toString()
                            });
                          }
                        }
                      } else if (thread.action === 'D') {
                        _ref3 = thread.old_data;
                        for (k in _ref3) {
                          v = _ref3[k];
                          if (v !== null && v.toString().length > 0) {
                            thread.modified_fields.push({
                              key: k,
                              old_v: v.toString()
                            });
                          }
                        }
                      } else if (thread.action === 'U') {
                        _ref4 = thread.new_data;
                        for (k in _ref4) {
                          v = _ref4[k];
                          v = v === null ? '' : v.toString();
                          old_v = thread.old_data[k] === null ? '' : thread.old_data[k].toString();
                          if (old_v !== v) {
                            thread.modified_fields.push({
                              key: k,
                              new_v: v,
                              old_v: old_v
                            });
                          }
                        }
                      }
                      if (thread.modified_fields.length === 0) {
                        continue;
                      }
                      append = false;
                      _ref5 = thread.modified_fields;
                      for (_k = 0, _len2 = _ref5.length; _k < _len2; _k++) {
                        field = _ref5[_k];
                        if (!((_ref6 = field.key) === 'batch_num' || _ref6 === 'trace_num')) {
                          append = true;
                          break;
                        }
                      }
                      if (append) {
                        term.threads.push(thread);
                      }
                    }
                    return _next();
                  });
                }
              };
              _while(__iced_k);
            })(function() {
              done();
              res.locals.pay_terms = table_const.pay_terms;
              return res.render('audit/index');
            });
          });
        });
      };
    })(this));
  });

}).call(this);
